//
//  KCChangePWController.swift
//  MyKC
//
//  Created by yy on 2017/2/23.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import SVProgressHUD
class KCChangePWController: UITableViewController {
    lazy var titleArray = [["原始密码:","新密码:","确认密码"],["请输入旧密码","请输入新密码","请再次输入新密码"]]
    var rightBarButtonItem :UIBarButtonItem?
    var titledisArray = ["","",""]
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}
// MARK: -  设置界面信息-----
extension KCChangePWController {
    internal func setupUI() {
        //设置导航栏
        setupTableView()
        //设置导航栏
        setupNavBar()
    }
    private func setupTableView() {
        tableView.tableFooterView = UITableView()
        let value = CGFloat(242.0 / 255)
        tableView.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        //注册nib文件
        tableView.register(UINib(nibName: "KCChangePwCell", bundle: nil), forCellReuseIdentifier: "KCChangePwCellId")
    }
    private func setupNavBar() {
        navigationItem.title = "修改密码"
        let rightBarButton = UIBarButtonItem(title: "修改", style: .plain, target: self, action: #selector(changePWBtnClick(sender:)))
        rightBarButtonItem = rightBarButton
        rightBarButtonItem?.isEnabled = false
        navigationItem.rightBarButtonItem = rightBarButtonItem
    }
    func changePWBtnClick(sender: UIBarButtonItem) -> Void {
        uploadData()
    }
}

// MARK: -  tableView的代理方法-------
extension KCChangePWController {
    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    // MARK: -  返回每行cell内容
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "KCChangePwCellId", for: indexPath) as! KCChangePwCell
        cell.row = indexPath.row
        setupCell(cell: cell, indexPath: indexPath)
        return cell
    }
    
    private func setupCell(cell: KCChangePwCell,indexPath: IndexPath) {
        cell.selectionStyle = .none
        cell.lbe_title.text = titleArray[0][indexPath.row]
        cell.tf_password.placeholder = titleArray[1][indexPath.row]
        cell.pwCellBlock = { [weak self]
            row,name in
            self?.titledisArray[row] = name
            var isKeyong: Bool = true
            for pwad in (self?.titledisArray)! {
                if (pwad as NSString).length == 0 {
                    isKeyong = false
                }
            }
            self?.rightBarButtonItem?.isEnabled = isKeyong
        }
    }
}


// MARK: -  提交网络数据----------------------//
extension KCChangePWController {
    internal func uploadData() {
        view.endEditing(true)
        if ((titledisArray[1] == titledisArray[2]) == false) {
            SVProgressHUD.showError(withStatus: "两次输入密码不同")
            return
        }
        SVProgressHUD.show()
        let dic = ["OldPassword":titledisArray[0],"NewPassword":titledisArray[2]]
        KCNetworkManager.sharedTools.request(method: .POST, urlString: "/api/Personal/ResetPassword", parameters: dic) { (response, error) in
            guard let responseObj = response as? NSDictionary else {
                SVProgressHUD.dismiss()
                return
            }
            SVProgressHUD.dismiss()
            if let string = responseObj["Msg"] as? String {
                SVProgressHUD.showSuccess(withStatus: string)
                let login = KCLoginViewController()
                UIApplication.shared.keyWindow?.rootViewController = login
            }
        }
    }
}

